package com.example.boot03.controller;

import com.example.boot03.entity.Product;
import com.example.boot03.util.DBUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
/*使用此注解。相当于每一个处理请求的方法上面都加了@ResponseBody 注解*/
@RestController
public class ProductController {
    @RequestMapping("/insert")
    //@ResponseBody
    public String insert(Product product) {
        System.out.println("product = " + product);
        try (Connection connection = DBUtil.getConnection()) {
            String sql="INSERT INTO product VALUES (null,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, product.getTitle());
            ps.setDouble(2, product.getPrice());
            ps.setInt(3, product.getNum());

            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "添加成功<a href='/'>返回首页</a>";
    }

    @RequestMapping("/select")
    //@ResponseBody
    public String select() {
        ArrayList<Product> list = new ArrayList<>();
        try (Connection connection = DBUtil.getConnection()) {
            String sql="SELECT id,title,price,num FROM product";
            PreparedStatement ps = connection.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                int id = rs.getInt(1);
                String title = rs.getString(2);
                double price = rs.getDouble(3);
                int num = rs.getInt(4);
                //把查询到的数据封装到对象里面
                Product p = new Product();
                p.setId(id);
                p.setTitle(title);
                p.setPrice(price);
                p.setNum(num);
                //把对象装进list集合
                list.add(p);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        /*装table 标签里*/
        String html = "<table border='1' style='color:blue'>";
        html+="<tr><th>id</th><th>标题</th><th>价格(元)</th><th>库存(件)</th><th>操作</th></tr>";
        for (Product p : list) {
            html+="<tr>";
            html+="<td>"+p.getId()+"</td>";
            html+="<td>"+p.getTitle()+"</td>";
            html+="<td>"+p.getPrice()+"</td>";
            html+="<td>"+p.getNum()+"</td>";
            html+="<td><a href='/delete?id="+p.getId()+"'>删除</a></td>";
            html+="</tr>";
        }
        html+="</table>";
        return html;
       /* return list.toString();*/
    }

    @RequestMapping("/delete")
    //@ResponseBody
    public String delete(int id) {
        System.out.println("id = " + id);
        try (Connection conn = DBUtil.getConnection()) {
            String sql = "DELETE FROM product WHERE id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "删除完成！<a href='/select'>返回列表页面</a>";
    }


    @RequestMapping("/update")
    //@ResponseBody
    public String update(Product product) {
        System.out.println("product = " + product);
        try (Connection conn = DBUtil.getConnection()) {
            String sql = "UPDATE  product SET title=?,price=?,num=? WHERE id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, product.getTitle());
            ps.setDouble(2, product.getPrice());
            ps.setInt(3, product.getNum());
            ps.setInt(4, product.getId());
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "修改成功<a href='/select'>返回列表页面</a>";
    }



}
